home *** CD-ROM | disk | FTP | other *** search
/ CDUTIL 13 / CDUTIL #13 Julio 1995.iso / windows / acadcom / ads / sample / calmngf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-08  |  5.3 KB  |  170 lines

  1. /* Next available MSG number is  1 */
  2.  
  3. /*****************************************************************************
  4.       CALMNGF.C
  5.       (C) Copyright 1988-1994 by Autodesk, Inc.
  6.  
  7.       This program is copyrighted by Autodesk, Inc. and is  licensed
  8.       to you under the following conditions.  You may not distribute
  9.       or  publish the source code of this program in any form.   You
  10.       may  incorporate this code in object form in derivative  works
  11.       provided  such  derivative  works  are  (i.) are  designed and 
  12.       intended  to  work  solely  with  Autodesk, Inc. products, and 
  13.       (ii.)  contain  Autodesk's  copyright  notice  "(C)  Copyright  
  14.       1988-1993 by Autodesk, Inc."
  15.  
  16.       AUTODESK  PROVIDES THIS PROGRAM "AS IS" AND WITH  ALL  FAULTS.
  17.       AUTODESK  SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF  MER-
  18.       CHANTABILITY OR FITNESS FOR A PARTICULAR USE.  AUTODESK,  INC.
  19.       DOES  NOT  WARRANT THAT THE OPERATION OF THE PROGRAM  WILL  BE
  20.       UNINTERRUPTED OR ERROR FREE.
  21.  
  22.   Description: Management of the table of calculator functions.
  23.  
  24. *****************************************************************************/
  25.  
  26.  
  27. /****************************************************************************/
  28. /*  INCLUDES                                                                */
  29. /****************************************************************************/
  30.  
  31. #include "cal.h"
  32.  
  33.  
  34. /****************************************************************************/
  35. /*  EXPORTED VARIABLES                                                      */
  36. /****************************************************************************/
  37.  
  38. cft cal_funcs_table;                  /* Table of calculator functions  */
  39. int cal_funcs_number = 0;             /* Number of calculator functions */
  40.  
  41. /* Variables for passing actual parameters to calculator functions 
  42.    and returning the value from the function:
  43.  
  44.    Before a calculator function is called, its acutal parameters must 
  45.    be stored in global array 'params[]'. The function returns its result
  46.    in global variable 'result'. See functions in module 'calstdf.c'.
  47. */
  48.  
  49. int             no_of_params;
  50. vector_real_int params[MAX_FUNC_PARAM];
  51. vector_real_int result;
  52.  
  53.  
  54. /****************************************************************************/
  55. /*.doc cal_register_function(external)*/
  56. /*+
  57.   Function registers new calculator function. 'func_name' is the
  58.   name of the function which will be used to refer to this function
  59.   in arithmetic expressions, 'func_ptr' is a pointer to the function 
  60.   definition. If the function fails, it prints an error message
  61.   and terminates the ADS application.
  62. -*/
  63. /****************************************************************************/
  64.  
  65.  
  66. void
  67. /*FCN*/cal_register_function(func_name, func_ptr)
  68.  
  69.   char *func_name;
  70.   void (*func_ptr)();
  71. {
  72.     int  i;
  73.     char name[MAX_SYMBOL_LENGTH+1];
  74.     int  len;
  75.  
  76.     if (cal_err)
  77.         return;
  78.  
  79.     /* Do initial checking */
  80.  
  81.     if (cal_funcs_number >= MAX_CAL_FUNC) {
  82.         error(44, NULL);
  83.         return;
  84.     }
  85.  
  86.     if (func_name == NULL) {
  87.         error(46, NULL);
  88.         return;
  89.     }
  90.  
  91.     len = strlen(func_name);
  92.  
  93.     if (len > MAX_SYMBOL_LENGTH) {
  94.         error(47, func_name);
  95.         return;
  96.     }
  97.  
  98.     /* Check whether the function name is an identifier */
  99.  
  100.     strcpy(name, func_name);
  101.  
  102.     if (!ads_isalpha(name[0])) {
  103.         error(42, func_name);
  104.         return;
  105.     }
  106.     for (i = 0; i < len; i++) {
  107.         name[i] = ads_toupper(name[i]);
  108.         if (!ads_isalnum(name[i]) && (name[i] != '_')) {
  109.             error(42, func_name);
  110.             return;
  111.         }
  112.     }
  113.  
  114.     /* Check whether function of this name wasn't already registered */
  115.  
  116.     for (i = 0; i < cal_funcs_number; i++) {
  117.         if (strcmp(cal_funcs_table[i].name, name) == 0) {
  118.             error(60, func_name);
  119.             return;
  120.         }
  121.     }
  122.  
  123.     /* Insert the new calculator function at the end of the table */
  124.  
  125.     strcpy(cal_funcs_table[cal_funcs_number].name, name);
  126.     cal_funcs_table[cal_funcs_number].func = func_ptr;
  127.     cal_funcs_number++;
  128.  
  129. } /*cal_register_function*/
  130.  
  131.  
  132. /****************************************************************************/
  133. /*.doc cal_invoke_function(external)*/
  134. /*+
  135.   Invoke calculator function whose name is 'func_name'. 
  136.  
  137.   Before a function is invoked, the function parameters must be stored in 
  138.   array'params[]', the number of actual parameters in 'no_of_params'. The 
  139.   invoked function returns its result in 'result'. 
  140.  
  141.   Function returns TRUE if function 'func_name' was successfully invoked
  142.   and the invoked function succeeded, otherwise FALSE is returned. You can
  143.   use 'cal_err' variable to check the reason why the invocation failed.
  144. -*/
  145. /****************************************************************************/
  146.  
  147.  
  148. int
  149. /*FCN*/cal_invoke_function(func_name)
  150.  
  151.   char *func_name;
  152. {
  153.     int i;
  154.  
  155.     if (cal_err)
  156.         return(FALSE);
  157.  
  158.     for (i = 0; i < cal_funcs_number; i++) {
  159.         if (strcmp(func_name, cal_funcs_table[i].name) == 0) {
  160.             (*cal_funcs_table[i].func)();
  161.             return(cal_err == 0);
  162.         }
  163.     } /*for*/
  164.  
  165.     error(52, func_name);
  166.     return(FALSE);
  167. } /*cal_invoke_function*/
  168.  
  169.  
  170.